#include <bits/stdc++.h>
using namespace std;
#define int long long
int dx[4]={0,-1,0,1};
int dy[4]={-1,0,1,0};
void solve()
{
    int n;
    cin>>n;
    vector<vector<int>> v(n+1,vector<int>(n+1,-1));
    if(n==1)cout<<0<<endl;
    else
    {
        int cnt=n*n-1,i=n,j=n,k=0;
        while(cnt!=-1)
        {
            v[i][j]=cnt--;
            int x=i+dx[k],y=j+dy[k];
            if(x<1||x>n||y<1||y>n||v[x][y]!=-1)
            {
                k=(k+1)%4;
                x=i+dx[k],y=j+dy[k];
            }
            i=x,j=y;
        }
        for(int m=1;m<=n;m++)
        {
            for(int z=1;z<=n;z++)
            {
                cout<<v[m][z]<<" ";
            }
            cout<<endl;
        }
    }
}
signed main()
{
    //ios::sync_with_stdio(false);
    //cin.tie(nullptr);
    int T=1;
    cin>>T;
    int cnt=0;
    while(T--)
    {
       solve();
    }
    return 0;
}